from lxml import etree
import requests


def search_bvid(keyword):
    headers = {
        "Cookie": "buvid3=9C1F8D49-2214-5CF6-B0D1-40090DBB707689075infoc; b_nut=1696490789; i-wanna-go-back=-1; b_ut=7; b_lsid=8B992C3A_18AFEBB1FD4; bsource=search_baidu; _uuid=9667A1010F-8DF10-1033F-F84A-4265A64354B1090882infoc; buvid4=82914101-12F7-17EE-C2A5-DA9D0805179595609-023100515-idORq%2Fc2IrwTKvPIfqriCg%3D%3D; header_theme_version=CLOSE; home_feed_column=5; browser_resolution=2103-1552; fingerprint=5c3a5c3efeb3398c02157d7c1c49871f; buvid_fp_plain=undefined; buvid_fp=5c3a5c3efeb3398c02157d7c1c49871f; rpdid=|(k|mlJ||~~l0J'uYmYRmlkRR; CURRENT_FNVAL=4048; sid=drxe6d6a; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTY3NTQ0NjcsImlhdCI6MTY5NjQ5NTIwNywicGx0IjotMX0.dHtWqgc5-1JBpA4ZIOLHUDTuoEqkyCF_zURO-WJvrPc; bili_ticket_expires=1696754407",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        'Accept-Encoding': "gzip, deflate, br",
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        "Cache-Control": "max-age=0",
        'Sec-Ch-Ua': 'Microsoft Edge";v="117", "Not;A=Brand";v="8", "Chromium";v="117',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': "Windows",
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.47'
    }

    url = 'https://search.bilibili.com/all'
    data = {
        'vt': '95194848',
        'keyword': keyword,
        'from_source': 'webtop_search',
        'spm_id_from': '333.1007',
        'search_source': '5'
    }

    response = requests.get(url=url, headers=headers, params=data)
    x = etree.HTML(response.text)
    bvid = x.xpath('//div[@class="bili-video-card__wrap __scale-wrap"]/a[@target="_blank"]/@href')  # bvid
    img_path = x.xpath('//div//img[@loading="lazy"]/@src')  # img
    img_path_l = []
    for i in img_path:
        a = str(i)
        if not a.startswith(("https")):
            img_path_l.append(i)

    bvid_list = []
    for i in bvid:
        bvid_list.append(i.split("/")[-2])

    return bvid_list, bvid,img_path_l
